package com.demo.controller;

import org.apache.commons.imaging.ImageFormats;
import org.apache.commons.imaging.ImageReadException;
import org.apache.commons.imaging.ImageWriteException;
import org.apache.commons.imaging.Imaging;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Graphics2DUtil {
    private static final String FONT_FAMILY = "楷体";
    private static final String CERTIFICATE_BASE_PATH = "/Users/wangfengchen/Downloads/test/img.gif";
//    private static final String WATERMARK_IMAGE_PATH = "/src/main/resources/static/icon.png";

    public static void graphics2DDrawTest(String srcImgPath, String outPath) {
        try {
//            BufferedImage targetImg = ImageIO.read(new File(srcImgPath));
            BufferedImage targetImg = Imaging.getBufferedImage(new File(srcImgPath));
            int imgWidth = targetImg.getWidth();
            int imgHeight = targetImg.getHeight();
            BufferedImage bufferedImage = new BufferedImage(imgWidth, imgHeight,
                BufferedImage.TYPE_INT_BGR);
            Graphics2D g = bufferedImage.createGraphics();

            g.drawImage(targetImg, 0, 0, imgWidth, imgHeight, null);
            g.setColor(Color.RED);
            // 第一行文本字体大小为120，居中显示
            Font userNameFont = new Font(FONT_FAMILY, Font.PLAIN, 120);
            g.setFont(userNameFont);

            String userName = "测试";
            int[] userNameSize = getContentSize(userNameFont, userName);

            int userNameLeftMargin = (imgWidth - userNameSize[0]) / 2;
            int userNameTopMargin = 150 + userNameSize[1];
            g.drawString(userName, userNameLeftMargin, userNameTopMargin);
            g.dispose();

            FileOutputStream outImgStream = new FileOutputStream(outPath);
//            ImageIO.write(bufferedImage, "png", outImgStream);
//            ImageIO.write(bufferedImage, "gif", outImgStream);
            Imaging.writeImage(bufferedImage, outImgStream, ImageFormats.GIF, null);
            g.dispose();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ImageWriteException e) {
            throw new RuntimeException(e);
        } catch (ImageReadException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取文本的长度，字体大小不同，长度也不同
     *
     * @param font
     * @param content
     * @return
     */
    public static int[] getContentSize(Font font, String content) {
        int[] contentSize = new int[2];
        FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, true);
        Rectangle rec = font.getStringBounds(content, frc).getBounds();
        contentSize[0] = (int) rec.getWidth();
        contentSize[1] = (int) rec.getHeight();
        return contentSize;
    }


    public static void main(String[] args) throws IOException {
        String projectPath = System.getProperty("user.dir");
        String srcImgPath = CERTIFICATE_BASE_PATH;
        String outPath = "/Users/wangfengchen/Downloads/test/success.gif";

        graphics2DDrawTest(srcImgPath, outPath);
    }


}